//
//  AwardsRecordVC.m
//  NormalFramework
//
//  Created by beisheng03 on 2018/3/16.
//  Copyright © 2018年 beisheng. All rights reserved.
//

#import "AwardsRecordVC.h"
#import "AwardRecordCell.h"
@interface AwardsRecordVC ()
@property (nonatomic, assign) NSInteger curpage;

@property (nonatomic, assign) NSString *pageCount;


@property (nonatomic, strong) NSMutableArray<NSObject *> *dataModels;
@end

@implementation AwardsRecordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    if (@available(iOS 11.0, *)) {
        
        
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        
    }
    self.tableView.rowHeight = 90;
    
    CGFloat top = IPhoneX ? 88 : 64;
    self.tableView.contentInset = UIEdgeInsetsMake(top + 135, 0, IPhoneX ? 34 : 0, 0);
    
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([AwardRecordCell class]) bundle:nil] forCellReuseIdentifier:[AwardRecordCell identifierStr]];
    
    
    MJWeakSelf
    self.tableView.mj_header = [XMGRefreshHeader headerWithRefreshingBlock:^{
        
        
        [weakSelf loadData];
        
    }];
    
    //    [self.tableView.mj_header beginRefreshing];
    
    
    self.tableView.mj_footer = [XMGRefreshFooter footerWithRefreshingBlock:^{
        
        [weakSelf loadMoreData];
        
    }];
    
    self.tableView.tableFooterView = [[UIView alloc]init];
    
    
    
}





- (void)loadData{
    
    
    self.curpage = 1;
    
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    
    parmars[@"curpage"] = @"1";
    
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/get_more" parameters:parmars completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            //            NSLog(@"%@",[responseObject mj_JSONString]);
            
            
            
            weakSelf.pageCount = responseObject[@"data"][@"count"];
            
            self.dataModels = [NSObject mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
            
            [weakSelf.tableView.mj_footer resetNoMoreData];
            
            if ([weakSelf.pageCount isEqualToString:@"1"]) {
                
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                
            }
            
            [weakSelf.tableView.mj_header endRefreshing];
            
            [weakSelf.tableView reloadData];
            
        } else {
            
            [weakSelf.tableView.mj_header endRefreshing];
            
            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
        
        
        
    }];
    
    
    
}






- (void)loadMoreData
{
    self.curpage ++;
    
    NSMutableDictionary *parmars = [NSMutableDictionary dictionary];
    
    parmars[@"curpage"] = [NSString stringWithFormat:@"%ld",(long)self.curpage];
    
    
    
    
    MJWeakSelf;
    [[NetworkTools shareNetworkTools]sendRequestWithHttpMethod:POST_METHOD URLPath:@"index.php/Api/Dbindex/get_more" parameters:parmars completionHandler:^(id responseObject, NSError *error) {
        
        if (!error) {
            
            
            NSMutableArray<NSObject *> *temp = [NSObject mj_objectArrayWithKeyValuesArray:responseObject[@"data"][@"list"]];
            
            [weakSelf.dataModels addObjectsFromArray:temp];
            
            if (weakSelf.pageCount.integerValue == self.curpage) {
                
                [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                
            } else {
                
                [weakSelf.tableView.mj_footer endRefreshing];
                
                
            }
            
            
            
            [weakSelf.tableView reloadData];
            
        } else {
            
            [weakSelf.tableView.mj_footer endRefreshing];
            
            [SVProgressHUD showErrorWithStatus:error.domain];
            
        }
        
        
        
    }];
    
    
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
    
    if (self.dataModels == nil) {
        return 0;
    }
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 100;
    if (self.dataModels == nil) {
        return 0;
    }
    return self.dataModels.count;
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AwardRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:[AwardRecordCell identifierStr] ];
    
    
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return 188;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
